﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectDirector.Domain.Entities;
using ProjectDirector.Domain.Abstracts;

namespace ProjectDirector.Domain.Concretes
{
    public class ProjectDirectorRepository : IProjectDirectorRepository
    {
        private ProjectDirectorModelDataContext ctx = null;

        public ProjectDirectorRepository(string connection) {
            ctx = new ProjectDirectorModelDataContext(connection);
        }

        public List<Country> GetCountries()
        {
            return ctx.Countries.ToList();
        }

        public List<JobTitle> GetJobTitles()
        {
            return ctx.JobTitles.ToList();
        }

        public List<ContractType> GetContractTypes()
        {
            return ctx.ContractTypes.ToList();
        }

        public List<BusinessType> GetBusinessTypes()
        {
            return ctx.BusinessTypes.ToList();
        }

        public List<Title> GetTitles() {
            return ctx.Titles.ToList();
        }

        public IQueryable<Employee> GetEmployees()
        {
            return ctx.Employees.AsQueryable<Employee>();
        }

        public IQueryable<Address> GetAddress() {
            return ctx.Addresses.AsQueryable<Address>();
        }        

        public IQueryable<Customer> GetCustomers()
        {
            return ctx.Customers.AsQueryable<Customer>();
        }

        public int AddEmployee(Employee employee, Address address)
        {
            try
            {
                if (address != null)
                {
                    ctx.Addresses.InsertOnSubmit(address);
                    SaveChanges();
                    if (address.AddressId > 0)
                    {
                        if (employee != null)
                        {
                            employee.AddressId = address.AddressId;
                            ctx.Employees.InsertOnSubmit(employee);
                            SaveChanges();
                            return (1);
                        }
                    }
                }
            }
            catch (Exception ex) { DiagnosticLog.LogError(ex); }
            return (0);
        }

        public int AddCustomer(Customer customer, Address address) {
            try {
                if (address != null)
                {
                    ctx.Addresses.InsertOnSubmit(address);
                    SaveChanges();
                    if (address.AddressId > 0)
                    {
                        if (customer != null)
                        {
                            customer.AddressId = address.AddressId;
                            ctx.Customers.InsertOnSubmit(customer);
                            SaveChanges();
                            return (1);
                        }
                    }
                }
            }
            catch (Exception ex) { DiagnosticLog.LogError(ex); }
            return (0);
        }

        public int UpdateEmployee(Employee employee) {
            try {
                if (employee != null)
                {
                    Employee updateEmployee = ctx.Employees.SingleOrDefault(x => x.UserId == employee.UserId);
                    if (updateEmployee != null)
                    {
                        updateEmployee.TitleId = employee.TitleId;
                        updateEmployee.FirstName = employee.FirstName;
                        updateEmployee.LastName = employee.LastName;
                        updateEmployee.StartDate = employee.StartDate;
                        if (employee.FinishDate.HasValue)
                            updateEmployee.FinishDate = employee.FinishDate.Value;
                        updateEmployee.JobTitleId = employee.JobTitleId;
                        updateEmployee.ContractTypeId = employee.ContractTypeId;
                        updateEmployee.WorkPhone = employee.WorkPhone;
                        updateEmployee.WorkEmail = employee.WorkEmail;
                        updateEmployee.MobilePhone = employee.MobilePhone;
                        updateEmployee.PrivateEmail = employee.PrivateEmail;
                        updateEmployee.UpdatedBy = employee.UpdatedBy;
                        updateEmployee.DateUpdated = employee.DateUpdated;

                        SaveChanges();
                        return (1);
                    }
                }
                    
            }
            catch (Exception ex) { DiagnosticLog.LogError(ex); }
            return (0);
        }

        public int UpdateAddress(Address address) {
            try
            {
                if (address != null)
                {
                    Address updateAddress = ctx.Addresses.SingleOrDefault(x => x.AddressId == address.AddressId);
                    if (updateAddress != null)
                    {
                        updateAddress.Address1 = address.Address1;
                        updateAddress.Address2 = address.Address2;
                        updateAddress.Town = address.Town;
                        updateAddress.PostalCode = address.PostalCode;
                        updateAddress.CountryId = address.CountryId;
                        SaveChanges();
                        return (1);
                    }
                }
            }
            catch (Exception ex) { DiagnosticLog.LogError(ex); }
            return (0);
        }

        public int DeleteEmployee(Employee employee) {
            try
            {
                if (employee != null)
                {
                    Employee deleteEmployee = ctx.Employees.SingleOrDefault(x => x.UserId == employee.UserId);
                    if (deleteEmployee != null)
                    {
                        deleteEmployee.IsDeleted = employee.IsDeleted;
                        deleteEmployee.UpdatedBy = employee.UpdatedBy;
                        deleteEmployee.DateUpdated = employee.DateUpdated;
                        SaveChanges();
                        return (1);
                    }
                }
            }
            catch (Exception ex) { DiagnosticLog.LogError(ex); }
            return (0);
        }

        public int DeleteCustomer(Customer _customer)
        {
            try
            {
                if (_customer != null)
                {
                    Customer _costomerToDelete = ctx.Customers.SingleOrDefault(x => x.CustomerId == _customer.CustomerId);
                    if (_costomerToDelete != null)
                    {
                        _costomerToDelete.IsDeleted = _customer.IsDeleted;
                        _costomerToDelete.UpdatedBy = _customer.UpdatedBy;
                        _costomerToDelete.DateUpdated = _customer.DateUpdated;
                        SaveChanges();
                        return (1);
                    }
                }
            }
            catch (Exception ex) { DiagnosticLog.LogError(ex); }
            return (0);
        }

        public int UpdateCustomer(Customer _customer, Address _address) {
            try
            {
                if (_customer != null && _address != null)
                {
                    Customer customerToUpdate = ctx.Customers.SingleOrDefault(x => x.CustomerId == _customer.CustomerId);
                    if (customerToUpdate != null)
                    {
                        customerToUpdate.CompanyName = _customer.CompanyName;
                        customerToUpdate.BusinessTypeId = _customer.BusinessTypeId;
                        customerToUpdate.Telephone = _customer.Telephone;
                        customerToUpdate.Email = (string.IsNullOrEmpty(_customer.Email) ? "N/A" : _customer.Email);
                        customerToUpdate.FaxNumber = (string.IsNullOrEmpty(_customer.FaxNumber) ? "N/A" : _customer.FaxNumber);
                        customerToUpdate.WebSite = (string.IsNullOrEmpty(_customer.WebSite) ? "N/A" : _customer.WebSite);
                        customerToUpdate.UpdatedBy = _customer.UpdatedBy;
                        customerToUpdate.DateUpdated = _customer.DateUpdated;
                        SaveChanges();
                    }
                    Address addressToUpdate = ctx.Addresses.SingleOrDefault(x => x.AddressId == _address.AddressId);
                    if (addressToUpdate != null)
                    {
                        addressToUpdate.Address1 = _address.Address1;
                        addressToUpdate.Address2 = _address.Address2;
                        addressToUpdate.Town = _address.Town;
                        addressToUpdate.PostalCode = _address.PostalCode;
                        addressToUpdate.CountryId = _address.CountryId;
                        SaveChanges();
                    }
                }
                return (1);
            }
            catch (Exception ex) { DiagnosticLog.LogError(ex); }
            return (0);        
        }

        public List<Contact> GetContactsByCustomer(int id)
        {
            return ctx.Contacts.Where(x => x.CustomerId == id).ToList();
        }

        private void SaveChanges()
        {
            ctx.SubmitChanges();
        }

        public int AddContact(Contact contact)
        {
            throw new NotImplementedException();
        }
    }
}
